package top.chukongxiang.project.base.service.sys.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.chukongxiang.project.base.mapper.sys.RoleMenuMapper;
import top.chukongxiang.project.base.model.entity.sys.Menu;
import top.chukongxiang.project.base.model.entity.sys.RoleMenu;
import top.chukongxiang.project.base.service.sys.RoleMenuService;
import top.chukongxiang.project.base.utils.ArrayUtils;

import java.util.List;

/**
 * 角色用户Service实现
 * @author 楚孔响
 * @version 1.0
 * @date 2022/2/21 16:39
 */
@Service
@Slf4j
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Override
    public boolean checkMenuIsBandRole(Long menuId) {
        return count(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getMenuId, menuId)) == 0;
    }

    @Override
    public List<Long> listMenuIdByRoleId(Long roleId) {
        return ArrayUtils.map(list(
                Wrappers.<RoleMenu>lambdaQuery()
                    .select(RoleMenu::getMenuId)
                    .eq(RoleMenu::getRoleId, roleId)),
                RoleMenu::getMenuId);
    }

    @Override
    public List<Menu> listMenuByRoleId(Long roleId) {
        return baseMapper.listMenuByRoleId(roleId);
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return remove(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, roleId));
    }

}
